#ifndef __SEARCH_H_
#define __SEARCH_H_

#include "data.h"

#define KEIL 0
/*----------------------- M A C R O S ----------------------------------------*/

/*********** 摄像头参数***********/
#define   MAX_VIDEO_POINT           162             //每行采集的点数
#define   MID_VIDEO_POINT           81              //图像中心点
#define   MAX_VIDEO_LINE	        60              //每场实际用到的行数

// 形态学处理，腐蚀和膨胀的阈值区间
#define   MAX_CORROSION_THRESHOLD   5
#define	  MIN_CORROSION_THRESHOLD   2

#define   BORDER_MIN				0
#define   BORDER_MAX				MAX_VIDEO_POINT-2

typedef struct      //图像数据结构  以左下角为坐标原点 向上为x，向右为y
{
	int16 x;
	int16 y;
}Int16_point;
typedef struct 		//向上为x向右为y 以(0,0)点为原点
{
	INT16S x;
	INT16S y;
}_POINT;


/*************图像处理***********/
#define   MAX_POINT_NUM                 80                  //边沿和中心最大个数
#define   WIDTH                         60                  //第一行白块宽度要求 根据前瞻以及看到的图像情况适当调整
#define   INIT_DATA			            250                 //无效数据

#define ClrBit(reg,bit)       reg &= ~(1 << bit)
#define SetBit(reg,bit)       reg |= (1 << bit)
#define ReverseBit(reg,bit)   reg ^= (1 << bit)
#define BitIsSet(reg,bit)     reg & (1 << bit)

#define Delay_T3_mS(x)			while(0);					//空语句(在上位机中不需要使用延时,也不需要赋P波值或者执行中断操作,故使用空语句代替)
#define SET_PWM_MOT(x)			while(0);					//空语句
#define DisableInterrupts()		while(0);					//空语句
#define LED_0_ON()				while(0);
#define LED_0_OFF()				while(0);

#define MIN(a,b)               ((a<b)?(a):(b))
#define MAX(a,b)               ((a>b)?(a):(b))
#define ABS(x)                 (((x)>0)?(x):(-(x)))
#define LIMT(x,y)			   ((x>y)?y:((x<-y)?-y:x))	 // 求极限
#define GetMaxNumAmongThreeNum(x,a,b)  MIN(MAX(x,a),b)   // 保证x是在a,b之间的数

// 声明为外部变量，使外部的原文件可使用
extern uint8 g_VideoImageData[MAX_VIDEO_LINE][MAX_VIDEO_POINT];  //图像原始数据，g_VideoImageData[m][n]代表图像的从上数第m行，从左数第n点，0为最黑，255为最白
extern int IMAGE_MIDDLE;
//==============================图像数据================================//
extern _POINT  g_CenterPosition[MAX_POINT_NUM];
extern _POINT  g_LeftEdge[MAX_POINT_NUM], g_RightEdge[MAX_POINT_NUM];
extern int          g_CenterNum;
extern uint16          g_LeftEdgeNum, g_RightEdgeNum;
extern int			g_DirectionControl;

void Search();
void PreDisposeImage(uint8(*Image)[MAX_VIDEO_POINT]);
void CarconControl();
void Binarization(uint8(*image)[MAX_VIDEO_POINT], int row, int col);
void OstuBinarization(uint8(*Image)[MAX_VIDEO_POINT], int kernel, int row, int col, float threshold);
uint8 GetStartPoint(uint8 StartRow);
void SearchBinary(uint16 BreakFlag, uint8(*image)[MAX_VIDEO_POINT], uint16* LeftStastic, uint16* RightStastic, _POINT LeftStartPoint, _POINT RightStartPoint, uint8* hightest);
void StateMachine();
void GetLeftBinary(uint16 LeftTotal);
void GetRightBinary(uint16 RightTotal);
void TransfromArray();

#endif

#include "search.h"
#include"math.h"
#if(KEIL)
#include "SEEKFREE_MT9V03X.h"
#endif


int IMAGE_MIDDLE = 130;
/********************************************变量声明***************************************************/
uint8        g_VideoImageData[MAX_VIDEO_LINE][MAX_VIDEO_POINT];  //图像原始数据，g_VideoImageData[m][n]代表图像的从上数第m行，从左数第n点，0为最黑，255为最白
//==============================图像数据================================//
_POINT  g_CenterPosition[MAX_POINT_NUM];
_POINT  g_LeftEdge[MAX_POINT_NUM], g_RightEdge[MAX_POINT_NUM];
int          g_CenterNum;
uint16          g_LeftEdgeNum, g_RightEdgeNum;
//==============================图像处理用变量==========================//
uint16       g_Start[10], g_End[10], g_SEnum;
uint8        g_SearchFlag = 1;
int          g_Cover;
uint8        g_CoverIndex[10], g_CoverNum;
//==============================控制用变量==============================//
int			g_DirectionControl = MID_VIDEO_POINT;
int			g_FormerDirectionControl = MID_VIDEO_POINT;
int			g_DirectionControlWhole = 0;
int			g_DirectionControlLine = 0;
//======================================================================//

//==============================八邻域相关变量==========================//
_POINT LeftStartPoint, RightStartPoint;  // 左右边开始的点
uint16 LeftLineGrowDirction[MAX_POINT_NUM], RightLineGrowDirction[MAX_POINT_NUM];  // 左右边线生长方向
uint8 Highest = 0;  // 最高点
_POINT LeftEdgeFinal[MAX_POINT_NUM], RightEdgeFinal[MAX_POINT_NUM];   // 左右线数组最终结果
//======================================================================//

//===========================状态机和相关标志位========================//


//=====================================================================//


/*自适应动态阈值范围，获得一个相对最佳的阈值*/
void IMAGE_MIDDLE_SET()
{
	// 大津法
	unsigned long Amount = 0;
	unsigned long PixelBack = 0;
	unsigned long PixelshortegralBack = 0;
	unsigned long Pixelshortegral = 0;
	signed long PixelshortegralFore = 0;
	signed long PixelFore = 0;
	float OmegaBack, OmegaFore, MicroBack, MicroFore, SigmaB, Sigma; // 类间方差
	signed short MinValue = 0, MaxValue = 255;
	unsigned char HistoGram[256] = { 0 };
	// g_VideoImageData[MAX_VIDEO_LINE][MAX_VIDEO_POINT]
	for (int i = 0; i < MAX_VIDEO_LINE; i++)
		for (int j = 0; j < MAX_VIDEO_POINT; j++)
			HistoGram[g_VideoImageData[i][j]]++;
	for (; MinValue < 256 && HistoGram[MinValue] == 0; MinValue++);
	for (; MaxValue > MinValue && HistoGram[MaxValue] == 0 && MaxValue != MinValue; MaxValue--);
	if (MaxValue == MinValue)
	{
		IMAGE_MIDDLE = MaxValue;
	}
	else
	{
		if ((MinValue + 1) == MaxValue)
		{
			IMAGE_MIDDLE = MinValue;
		}
		else
		{
			Pixelshortegral = 0;
			SigmaB = -1;
			for (int i = MinValue; i <= MaxValue; i++)
			{
				Amount += HistoGram[i];  // 像素点总数
				Pixelshortegral += HistoGram[i] * i;  // 灰度值总数
			}
			float tmp = Pixelshortegral;
			float average_pixel = tmp / Amount;
			for (int i = MinValue; i <= MaxValue; i++)
			{
				PixelBack = PixelBack + HistoGram[i];  // 前景像素点数
				PixelFore = Amount - PixelBack;  // 背景像素点数
				OmegaBack = ((float)PixelBack) / Amount;  // 前景像素百分比
				OmegaFore = ((float)PixelFore) / Amount;  // 背景像素百分比
				PixelshortegralBack += HistoGram[i] * i; // 前景灰度值
				PixelshortegralFore = Pixelshortegral - PixelshortegralBack;  // 背景灰度值
				MicroBack = ((float)PixelshortegralBack) / PixelBack;  // 前景灰度百分比
				MicroFore = ((float)PixelshortegralFore) / PixelFore;  // 背景灰度百分比
				Sigma = PixelBack * (MicroBack - average_pixel) * (MicroBack - average_pixel) + PixelFore * (MicroFore - average_pixel) * (MicroFore - average_pixel);  // 类间方差
				if (Sigma > SigmaB)
				{
					SigmaB = Sigma;
					IMAGE_MIDDLE = i;
				}
			}
		}
	}
}


/*********	search:处理数据即测试算法的函数 ********/
void Search()
{
	//======================图像阈值及二值化====================//
	IMAGE_MIDDLE_SET();  //大津法，目前来说效果还好，如果改成局部大津法，运算时间会比较久
	// OstuBinarization(g_VideoImageData, 3, MAX_VIDEO_LINE, MAX_VIDEO_POINT, 1.5); // 局部大津法
	Binarization(g_VideoImageData, MAX_VIDEO_LINE, MAX_VIDEO_POINT);  // 二值化
	//=========================================================//

	//======================图像预处理和图像二值化=============//
	PreDisposeImage(g_VideoImageData);  // 预处理

	//=========================================================//


	//======================左右边线寻线========================//
	g_LeftEdgeNum = 0;
	g_RightEdgeNum = 0;
	if (GetStartPoint(MAX_VIDEO_LINE - 2))
	{
		printf("正在开始八邻域扫线\n");
		SearchBinary(MAX_VIDEO_LINE * 3, g_VideoImageData, &g_LeftEdgeNum, &g_RightEdgeNum, LeftStartPoint, RightStartPoint, &Highest); // 基础寻线
		GetLeftBinary(g_LeftEdgeNum);
		GetRightBinary(g_RightEdgeNum);
		TransfromArray();
		printf("八邻域扫线结束\n");
	}
	printf("g_LeftEdgeNum:%d,g_RightEdgeNum:%d", g_LeftEdgeNum, g_RightEdgeNum);
	//=========================================================//


	//=====传给控制的控制参量计算和赋值以及标志位赋值==========//


	//=========================================================//


	//========================中线计算=========================//


	//=========================================================//


	//===================控制中心及电机控制====================//
	CarconControl();
	//=========================================================//
}

/*图像预处理*/
void PreDisposeImage(uint8 (*Image)[MAX_VIDEO_POINT])
{
	for (int i = 1; i < MAX_VIDEO_LINE-1; i++)
	{
		for (int j = 1; j < MAX_VIDEO_POINT-1; j++)
		{
			// 统计相邻的八个像素点
			int sumPixels = Image[i - 1][j] + Image[i + 1][j] + Image[i - 1][j + 1] + Image[i + 1][j - 1] +
				Image[i][j + 1] + Image[i][j - 1] + Image[i + 1][j + 1] + Image[i - 1][j - 1];
			if (sumPixels > MAX_CORROSION_THRESHOLD && Image[i][j] == 0)
				Image[i][j] = 1;
			if (sumPixels < MIN_CORROSION_THRESHOLD && Image[i][j])
				Image[i][j] = 0;
		}
	}
}

/*电机控制*/
void CarconControl()
{
	//加权平均法求方向控制
	g_DirectionControlWhole = 0;
	g_DirectionControlLine = 0;
	for (int i = 0; i < g_CenterNum; i++)
	{
		if (g_CenterPosition[i].y >= 0 && g_CenterPosition[i].y <= MAX_VIDEO_POINT)
		{
			g_DirectionControlLine += (int)g_CenterPosition[i].x;
			g_DirectionControlWhole += (int)g_CenterPosition[i].y * g_CenterPosition[i].x;  //注意数据不要溢出
		}
	}
	if (g_DirectionControlLine > 0)
	{
		g_DirectionControl = g_DirectionControlWhole / g_DirectionControlLine;
	}
	//方向控制限定
	if (g_DirectionControl < 0)
	{
		g_DirectionControl = 0;
	}
	else if (g_DirectionControl > MAX_VIDEO_POINT)
	{
		g_DirectionControl = MAX_VIDEO_POINT;
	}
	if (ABS(g_DirectionControl - g_FormerDirectionControl) > 90)
	{
		g_DirectionControl = g_FormerDirectionControl;
	}
}

/*局部自适应阈值与二值化*/
void OstuBinarization(uint8 (*Image)[MAX_VIDEO_POINT], int kernel, int row, int col, float threshold)
{
	// 核心为3*3
	int adaptThreshold = (col > row ? col : row) / 10;
	int sumPixels = adaptThreshold / 2;
	int x1Pixels, y1Pixels, x2Pixels, y2Pixels, count;
	uint8* sumImage = NULL;
	sumImage = (uint8*)calloc(((col + 1) * (row + 1)), sizeof(int));
	if (sumImage != NULL)
	{
		// 计算获取积分图像
		for (int i = 0; i < row; i++)
		{
			uint8* inputRowPtr = (uint8*)Image[i * col];
			uint8* outputRowPtr = &sumImage[(i + 1) * col + 1];
			for (int j = 0; j < col; j++)
			{
				outputRowPtr[j] = 0;
				outputRowPtr[j] = outputRowPtr[j - 1] + outputRowPtr[j];
				outputRowPtr[j] += outputRowPtr[j - col] - outputRowPtr[j - col - 1];
			}
		}
		for (int y = 0; y < row; y++)
		{
			y1Pixels = y - sumPixels;
			y2Pixels = y + sumPixels;
			y1Pixels = y1Pixels < 0 ? 0 : y1Pixels;
			y2Pixels = y2Pixels > (row - 1) ? (row - 1) : y2Pixels;
			uint8* rowPtr = (uint8*)Image[y * col];
			uint8* y1_ptr = &sumImage[y1Pixels * col];
			uint8* y2_ptr = &sumImage[y2Pixels * col];
			for (int x = 0; x < col; x++)
			{
				x1Pixels = x - sumPixels;
				x2Pixels = x + sumPixels;
				x1Pixels = x1Pixels < 0 ? 0 : x1Pixels;
				x2Pixels = x2Pixels > (row - 1) ? (row - 1) : x2Pixels;
				count = (x2Pixels - x1Pixels) * (y2Pixels - y1Pixels);
				if (count > 0)
				{
					int summaltion = y2_ptr[x2Pixels] + y1_ptr[x1Pixels] - y1_ptr[x1Pixels] - y2_ptr[x2Pixels];
					printf("count:%d\n", count);
					uint8 thre = summaltion / count;
					rowPtr[x] = (((int)(rowPtr[x] * count)) > (int)(summaltion * (1.6f - threshold))) ? 255 : 0;
				}
			}
		}
		free(sumImage);
	}
	else
	{
		printf("未申请到内存空间");
	}
}

/*图像二值化*/
void Binarization(uint8 (*image)[MAX_VIDEO_POINT], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (image[i][j] <= IMAGE_MIDDLE)
				image[i][j] = 0;
			else
				image[i][j] = 1;
		}
	}
}

/*获取开始的点*/
uint8 GetStartPoint(uint8 StartRow)
{
	// 利用跳变找起点
	bool LeftFounrPoint = 0, RightFoundPoint = 0; // 左右边界点是否找到的标志
	LeftStartPoint.x = LeftStartPoint.y = 0;
	RightStartPoint.x = RightStartPoint.y = 0;
	for (int i = MAX_VIDEO_POINT / 2; i < MAX_VIDEO_POINT - 2; i++)
	{
		// 左边线起点
		if (g_VideoImageData[StartRow][i] == 1 && g_VideoImageData[StartRow][i - 1] == 0)
		{
			LeftFounrPoint = 1;
			LeftStartPoint.x = StartRow;
			LeftStartPoint.y = i;
			break;
		}
	}
	for (int i = MAX_VIDEO_POINT / 2; i > 0; i--)
	{
		// 右边线
		if (g_VideoImageData[StartRow][i] == 1 && g_VideoImageData[StartRow][i + 1] == 0)
		{
			RightFoundPoint = 1;
			RightStartPoint.x = StartRow;
			RightStartPoint.y = i;
			break;
		}
	}
	printf("左线起点横坐标:%d,左线起点纵坐标%d;右线起点横坐标:%d,右线起点纵坐标:%d\n", LeftStartPoint.x, LeftStartPoint.y, RightStartPoint.x, RightStartPoint.y);
	if (LeftFounrPoint && RightFoundPoint)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/*基础的搜索边线*/
void SearchBinary(uint16 BreakFlag, uint8(*image)[MAX_VIDEO_POINT], uint16* LeftStastic,
	uint16* RightStastic, _POINT LeftStartPoint, _POINT RightStartPoint, uint8* hightest)
{
	uint8 LoopVariate_i = 0, LoopVariate_j = 0;   // 循环变量
	_POINT LeftSearchFilds[8], RightSearchFilds[8];  // 左右边线的寻线区域
	uint8 LeftIndex = 0, RightIndex = 0;   // 左右边线索引标签
	_POINT LeftTemp[8], RightTemp[8];  // 临时的坐标数组
	_POINT LeftCenterPoint, RightCenterPoint;  // 左右线条寻线中心点坐标
	uint16 LeftDataStatics, RightDataStatics;  // 左右边线计数
	// 八个邻域
	static int8 LeftSeed[8][2] = { {0,  1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,  0},{1, 1} };
	static int8 RightSeed[8][2] = { {0,  1},{1,1},{1,0}, {1,-1},{0,-1},{-1,-1}, {-1,  0},{-1, 1} };

	// 统计找到的点数
	LeftDataStatics = *LeftStastic;
	RightDataStatics = *RightStastic;

	// 中心点赋值
	LeftCenterPoint.x = LeftStartPoint.x;
	LeftCenterPoint.y = LeftStartPoint.y;
	RightCenterPoint.x = RightStartPoint.x;
	RightCenterPoint.y = RightStartPoint.y;

	// 邻域循环
	while (BreakFlag--)
	{
		for (LoopVariate_i = 0; LoopVariate_i < 8; LoopVariate_i++)
		{
			// 左边线
			LeftSearchFilds[LoopVariate_i].y = LeftCenterPoint.y + LeftSeed[LoopVariate_i][0];
			LeftSearchFilds[LoopVariate_i].x = LeftCenterPoint.x + LeftSeed[LoopVariate_i][1];
			
			// 右边线
			RightSearchFilds[LoopVariate_i].y = RightCenterPoint.y + RightSeed[LoopVariate_i][0];
			RightSearchFilds[LoopVariate_i].x = RightCenterPoint.x + RightSeed[LoopVariate_i][1];
		}

		// 填充坐标点
		g_LeftEdge[LeftDataStatics].x = LeftCenterPoint.x;
		g_LeftEdge[LeftDataStatics].y = LeftCenterPoint.y;
		g_RightEdge[RightDataStatics].x = RightCenterPoint.x;
		g_RightEdge[RightDataStatics].y = RightCenterPoint.y;
		LeftDataStatics++;

		LeftIndex = 0;
		for (LoopVariate_i = 0; LoopVariate_i < 8; LoopVariate_i++)
		{
			LeftTemp[LoopVariate_i].x = 0;
			LeftTemp[LoopVariate_i].y = 0;
		}

		//左线
		for (LoopVariate_i = 0; LoopVariate_i < 8; LoopVariate_i++)
		{
			if (image[LeftSearchFilds[LoopVariate_i].y][LeftSearchFilds[LoopVariate_i].x] == 0
				&& image[LeftSearchFilds[(LoopVariate_i + 1) & 7].y][LeftSearchFilds[(LoopVariate_i + 1) & 7].x] == 1)
			{
				LeftTemp[LeftIndex].x = LeftSearchFilds[LoopVariate_i].x;
				LeftTemp[LeftIndex].y = LeftSearchFilds[LoopVariate_i].y;
				LeftIndex++;
				LeftLineGrowDirction[LeftDataStatics - 1] = LoopVariate_i;  // 生长方向
			}

			if (LeftIndex)
			{
				// 更新坐标点
				LeftCenterPoint.x = LeftTemp[0].x;
				LeftCenterPoint.y = LeftTemp[0].y;
				for (LoopVariate_j = 0; LoopVariate_j < LeftIndex; LoopVariate_j++)
				{
					if (LeftCenterPoint.y > LeftTemp[LoopVariate_j].y)
					{
						LeftCenterPoint.x = LeftTemp[LoopVariate_j].x;
						LeftCenterPoint.y = LeftTemp[LoopVariate_j].y;
					}
				}
			}
		}

		if ((g_RightEdge[RightDataStatics].x == g_RightEdge[RightDataStatics - 1].x && g_RightEdge[RightDataStatics].x == g_RightEdge[RightDataStatics - 2].x
			&& g_RightEdge[RightDataStatics].y == g_RightEdge[RightDataStatics - 1].y && g_RightEdge[RightDataStatics].y == g_RightEdge[RightDataStatics - 2].y)
			|| (g_LeftEdge[LeftDataStatics - 1].x == g_LeftEdge[LeftDataStatics - 2].x && g_LeftEdge[LeftDataStatics].x == g_LeftEdge[LeftDataStatics - 3].x
				&& g_LeftEdge[LeftDataStatics - 1].x == g_LeftEdge[LeftDataStatics - 2].y && g_LeftEdge[LeftDataStatics].y == g_LeftEdge[LeftDataStatics - 3].y))
		{
			printf("三次进入同一个点\n");
			break;
		}
		if (ABS(g_RightEdge[RightDataStatics].x - g_LeftEdge[LeftDataStatics - 1].x) < 2 && ABS(g_RightEdge[RightDataStatics].y - g_LeftEdge[LeftDataStatics - 1].y) < 2)
		{
			// 取出最高点
			*hightest = (g_RightEdge[RightDataStatics].y + g_LeftEdge[LeftDataStatics - 1].y) >> 1;
			printf("左右相遇退出\n");
			printf("在第%d行退出\n", *hightest);
			break;
		}
		if (g_RightEdge[RightDataStatics].y < g_LeftEdge[LeftDataStatics - 1].y)
		{
			printf("如果左边比右边高了，左边等待右边\n");
			continue;
		}
		
		//左边比右边高且已经向下生长了
		if (LeftLineGrowDirction[LeftDataStatics - 1] == 7 && (g_RightEdge[RightDataStatics].y > g_LeftEdge[LeftDataStatics - 1].y))
		{
			printf("左边开始向下了，等待右边，等待中... \n");
			LeftCenterPoint.x = g_LeftEdge[LeftDataStatics - 1].x;
			LeftCenterPoint.y = g_LeftEdge[LeftDataStatics - 1].y;
			LeftDataStatics--;
		}
		RightDataStatics++;

		RightIndex = 0;
		for (LoopVariate_i = 0; LoopVariate_i < 8; LoopVariate_i++)
		{
			RightTemp[LoopVariate_i].x = 0;
			RightTemp[LoopVariate_i].y = 0;
		}

		// 右边
		for (LoopVariate_i = 0; LoopVariate_i < 8; LoopVariate_i++)
		{
			if (image[RightSearchFilds[LoopVariate_i].y][RightSearchFilds[LoopVariate_i].x] == 0 && image[RightSearchFilds[(LoopVariate_i + 1) & 7].y][RightSearchFilds[(LoopVariate_i + 1) & 7].y] == 1)
			{
				RightTemp[RightIndex].x = RightSearchFilds[LoopVariate_i].x;
				RightTemp[RightIndex].y = RightSearchFilds[LoopVariate_i].y;
				RightIndex++;
				RightLineGrowDirction[RightDataStatics - 1] = LoopVariate_i;  // 生长方向
			}

			if (RightIndex)
			{
				// 更新坐标点
				RightCenterPoint.x = RightTemp[0].x;
				RightCenterPoint.y = RightTemp[0].y;
				for (LoopVariate_j = 0; LoopVariate_j < RightIndex; LoopVariate_j++)
				{
					if (RightCenterPoint.y > RightTemp[LoopVariate_j].y)
					{
						RightCenterPoint.x = RightTemp[LoopVariate_j].x;
						RightCenterPoint.y = RightTemp[LoopVariate_j].y;
					}
				}
			}
		}
	}
	// 取出循环次数
	*LeftStastic = LeftDataStatics;
	*RightStastic = RightDataStatics;
}

/*状态机函数*/
void StateMachine()
{

}

/*获取左边线*/
void GetLeftBinary(uint16 LeftTotal)
{
	for (int i = 0; i < MAX_VIDEO_LINE; i++)
	{
		LeftEdgeFinal[i].x = i;
		LeftEdgeFinal[i].y = BORDER_MIN;
	}
	int h = MAX_VIDEO_LINE - 2;
	for (int i = 0; i < LeftTotal; i++)
	{
		if (g_LeftEdge[i].x == h)
		{
			LeftEdgeFinal[h].y = g_LeftEdge[i].y + 1;
		}
		else
			continue;
		h--;
		if (h == 0)
		{
			break;
		}
	}
}

/*获取右边边线*/
void GetRightBinary(uint16 RightTotal)
{
	for (int i = 0; i < MAX_VIDEO_LINE; i++)
	{
		RightEdgeFinal[i].x = i;
		RightEdgeFinal[i].y = BORDER_MAX;
	}
	int h = MAX_VIDEO_LINE - 2;
	for (int i = 0; i < RightTotal; i++)
	{
		if (g_RightEdge[i].x == h)
		{
			RightEdgeFinal[h].y = g_RightEdge[i].y - 1;
		}
		else
			continue;
		h--;
		if (h == 0)
		{
			break;
		}
	}
}

/*转换数组*/
void TransfromArray()
{
	for (int i = 0; i < g_LeftEdgeNum; i++)
	{
		g_LeftEdge[i].x = LeftEdgeFinal[i].x;
		g_LeftEdge[i].y = LeftEdgeFinal[i].y;
	}
	for (int i = 0; i < g_RightEdgeNum; i++)
	{
		g_RightEdge[i].x = RightEdgeFinal[i].x;
		g_RightEdge[i].y = RightEdgeFinal[i].y;
	}
}